package org.example.modules.user.service.Impl;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.example.common.utils.JwtUtils;
import org.example.common.utils.ThreadLocalUtil;
import org.example.modules.user.common.Commsult;
import org.example.modules.user.entry.User;
import org.example.modules.user.entry.vo.InfoDetailsVo;
import org.example.modules.user.entry.vo.JobFeedBackVo;
import org.example.modules.user.mapper.ReviewMapper;
import org.example.modules.user.mapper.SideJobMainMapper;
import org.example.modules.user.mapper.UserMapper;
import org.example.modules.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SideJobMainMapper sideJobMainMapper;

    @Autowired
    private ReviewMapper reviewMapper;

//    @Autowired
//    private JwtTokenUtil jwtTokenUtil;
//
//    @Override
//    public String wxLogin(JSONObject data) {
//        log.info("data:{}",data);
//        User user = userMapper.selectByUserName(data.getString("userName"));
//        if(!user.getUserName().equals(data.getString("userName"))||!user.getPassword().equals(data.getString("password"))){
//            throw new RuntimeException("用户名或密码不正确");
//        }
//        return jwtTokenUtil.generateToken(user.getUserName(), "optionalAccessTokenValue");
//    }

    @Override
    public String wxLogin(JSONObject data) {
        User user = userMapper.selectByUserName(data.getString("userName"));

        if (!user.getUserName().equals(data.getString("userName")) || !user.getPassword().equals(data.getString("password"))) {
            return "用户名或密码不正确";
        }
        if(user.getStatus()!=0){
            return "该账号已被冻结请联系管理员";
        }
        // 验证成功，允许登陆，创建会话并发放会话 token
        String userIdStr = user.getUserName();
        return JwtUtils.createJWT(userIdStr);
    }


    @Override
    public Object userRegister(JSONObject data) {
        log.info("data:{}", data);
        User user = new User();
        user.setUserName(data.getString("userName"));
        user.setPassword(data.getString("password"));
        user.setPhone(data.getString("phone"));
        user.setGender(data.getInteger("gender"));
        user.setNickName(data.getString("nickName"));
        user.setAvatarUrl(data.getString("avatarUrl"));
        user.setUpdateTime(String.valueOf(LocalDateTime.now()));
        String createTime = data.getString("createTime");
        user.setAuthority(1);
        user.setStatus(0);
        if (createTime == null) {
            //User user1 = userMapper.selectByUserName(user.getUserName());
            User user1 = userMapper.selectUser(user.getUserName());
            if (user1 != null) {
                throw new RuntimeException("该账号已存在");
            }
            user.setCreateTime(String.valueOf(LocalDateTime.now()));
            userMapper.insert(user);
        } else {
            user.setId(data.getInteger("id"));
            user.setCreateTime(String.valueOf(LocalDateTime.parse(createTime)));
            userMapper.updateById(user);
        }
        return null;
    }

    @Override
    public List<User> getUserList() {
        return userMapper.selectUserList();
    }

    @Override
    public void updateUserStatus(JSONObject data) {
        Integer status = data.getInteger("status");
        Integer Id = data.getInteger("id");
        if (status == 1) {
            status = 0;
        } else {
            status = 1;
        }
        userMapper.updateStatusById(Id, status);
    }

    @Override
    public User getUserInfoByUserName(String userName) {
        String user = "";
        if (userName.contains("=")) {
            user = userName.substring(9);
        } else {
            user = userName;
        }

        return userMapper.getUserInfo(user);
    }

    @Override
    public User updateIngo(User userInfo) {
        String userName = ThreadLocalUtil.getUserId();
        if(userInfo.getAvatarUrl()!=null){
            return userMapper.selectUser(userName);
        }
        userMapper.updateById(userInfo);
        User user = userMapper.selectUser(userName);
        return user;
    }

    @Override
    public Commsult updatePassword(JSONObject data) {
        String userName = data.getString("userName");
        String phone = data.getString("phoneNumber");
        String password = data.getString("newPassword");
        User user = userMapper.selectUser(userName);
        if(user.getUserName()==null||!user.getPhone().equals(phone)){
            return Commsult.fail("账号与手机号不对应");
        }
        user.setPassword(password);
        userMapper.updateById(user);
        return  Commsult.success("重置密码成功");
    }

    @Override
    public void updateUserInfo(JSONObject data) {
        User user = data.toJavaObject(User.class);
        user.setUpdateTime(String.valueOf(LocalDateTime.now()));
        // 打印日志，验证结果
        userMapper.updateById(user);
    }

    @Override
    public void updateAuthority(String userName, Integer authority) {
        userMapper.updateAuthority(userName, authority);
    }

    @Override
    public InfoDetailsVo getUserInfo(JSONObject data) {
        String userName = data.getString("userName");
        User user = userMapper.getUserInfo(userName);
        InfoDetailsVo infoDetailsVo = new InfoDetailsVo();
        BeanUtils.copyProperties(user,infoDetailsVo);
        List<JobFeedBackVo>  list= new ArrayList<>();
        infoDetailsVo.setInfoList(sideJobMainMapper.getJobFeedBackList(userName));
        for(JobFeedBackVo jobFeedBackVo : infoDetailsVo.getInfoList()){
            Integer Id = jobFeedBackVo.getId();
            log.info("Id:{},userName:{}",Id,userName);
            jobFeedBackVo.setContent(reviewMapper.getReviewContent(Id,userName));
            list.add(jobFeedBackVo);
        }
        infoDetailsVo.setInfoList(list);
        return infoDetailsVo;
    }

    @Override
    public Commsult resetPassword(JSONObject data) {
        Integer id = data.getInteger("id");
        userMapper.resetPassword(id,"73998e7102c6c424ffe12067f38a8c29189b9c1230a791a5cda3d3daceeaccd3");
        return Commsult.success(null);
    }


}